package com.base.myproject.server;

import java.io.IOException;
import java.util.Vector;

import org.apache.commons.fileupload.FileItem;

public class SampleServlet  extends UploadAction {

	  private static final String SMTP_SERVER = "your_smtp_server";
	  private static final String MAIL_ADDRESS_FROM = "from_email@address";
	  private static final String MAIL_ADDRESS_TO = "to_email@address";

	  private static final long serialVersionUID = 1L;

	  @Override
	  public String doAction(Vector<FileItem> sessionFiles) throws IOException {
	   // try {
	      // create a new multipart content
	    //  MimeMultipart multiPart = new MimeMultipart();
	      String bodyTxt = "** Form content: \n\n";
	      for (FileItem item : sessionFiles) {
	        if (item.isFormField()) {
	          bodyTxt += "name: " + item.getFieldName() + " value: " + item.getString() + "\n";
	        } else {
	          bodyTxt += "name: " + item.getFieldName() + " filename: " + item.getName()  + " size: " + item.getSize() + "\n";
	          // add the file part to multipart content
	        //  MimeBodyPart part = new MimeBodyPart();
//	          part.setFileName(item.getName());
//	          part.setDataHandler(new DataHandler(new ByteArrayDataSource(item.get(), item.getContentType())));
//	          multiPart.addBodyPart(part);
	        }
	        System.out.println(bodyTxt);
	      }
	      // add the text part to multipart content
//	      MimeBodyPart txtPart= new MimeBodyPart();
//	      txtPart.setContent(bodyTxt, "text/plain");
//	      multiPart.addBodyPart(txtPart);
//	      
//	      // configure smtp server
//	      Properties props = System.getProperties();
//	      props.put("mail.smtp.host", SMTP_SERVER);
//	      // create a new session and the mime message
//	      MimeMessage mime = new MimeMessage(Session.getInstance(props));
//	      mime.setText(bodyTxt);
//	      mime.setContent(multiPart);
//	      mime.setFrom(new InternetAddress(MAIL_ADDRESS_FROM));
//	      mime.setRecipient(Message.RecipientType.TO, new InternetAddress(MAIL_ADDRESS_TO));
//	      mime.setSubject("New form submitted");
//	      // send the message
//	      Transport.send(mime);
//	    } catch (MessagingException e) {
//	      return super.stackTraceToString(e);
//	    }
	    // This is OK, null means no error.
	    // There is a bug in version of the library prior to 0.3.2
	    return null;
	  }

	}